<?php
	
	
if(!(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest")){ 
  echo returnJson(500,'非法请求！');die;
};



$dbms='mysql';       //数据库类型
$host='localhost';   //数据库主机名
$dbName='tubatu';    //使用的数据库
$user='root';        //数据库连接用户名
$pass='root';        //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";

try {
    $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    
    $sql = "INSERT INTO `form` SET name=:name,phone=:phone,school=:school,profession=:profession,time=:time,ip=:ip";
    $stmt = $pdo->prepare($sql);
	$result = $stmt->execute(array(
		':name' => htmlspecialchars($_POST['name']),
		':phone' => htmlspecialchars($_POST['phone']),
		':school' => htmlspecialchars($_POST['school']),
		':profession' => htmlspecialchars($_POST['profession']),
		':time' => time(),
		':ip' => $_SERVER["REMOTE_ADDR"],
	));
    $pdo = null;
    if($result){
    	echo returnJson(200,'提交成功',$result);die;
    }else{
    	echo returnJson(500,'提交失败');die;
    }
    
} catch (PDOException $e) {
    echo returnJson(500,'error',$e->getMessage());die;
}

// 返回json格式
function returnJson($code = 404 ,$msg = null,$data = null){
	return json_encode(array(
		'code' => $code,
		'msg' => $msg,
		'data' => $data
	));
}
?>